//
//  SelfModifyNicknameViewController.m
//  customer
//
//  Created by Holothurian on 2018/10/24.
//  Copyright © 2018年 yinlong. All rights reserved.
//

#import "SelfModifyNicknameViewController.h"

static NSString *placeHolder = @"用户昵称最长10个中文字符";
@interface SelfModifyNicknameViewController ()<UITextFieldDelegate>

@property (nonatomic,strong)UITextField *textField;

@end

@implementation SelfModifyNicknameViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = @"修改昵称";
    [self createView];
}

- (void)createView{
    [self.view addSubview:self.textField];
    UILabel *tinyLabel = [[UILabel alloc]init];
    tinyLabel.text = placeHolder;
    tinyLabel.textColor = [HSPublic colorWithHexString:@"333333"];
    tinyLabel.alpha = 0.4;
    tinyLabel.font = Font(12);
    [self.view addSubview:tinyLabel];
    AutoWeakSelf
    [tinyLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(weakSelf.textField.mas_bottom).offset(10);
        make.left.equalTo(weakSelf.view).offset(30);
        make.size.mas_equalTo(CGSizeMake(F_DEVICE_W - 60, 30));
    }];
    UIButton *modifyButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [modifyButton setTitle:@"修改昵称" forState:UIControlStateNormal];
    [modifyButton setTitle:@"修改昵称" forState:UIControlStateSelected];
    [modifyButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [modifyButton setTitleColor:[UIColor whiteColor] forState:UIControlStateSelected];
    [modifyButton setBackgroundColor:[HSPublic colorWithHexString:@"333333"]];
    modifyButton.titleLabel.font = Font(16);
    modifyButton.layer.cornerRadius = 10;
    modifyButton.layer.masksToBounds = YES;
    [modifyButton addTarget:self action:@selector(modifyButtonAction) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:modifyButton];
    [modifyButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(tinyLabel.mas_bottom).offset(90);
        make.left.equalTo(weakSelf.view).offset(15);
        make.right.equalTo(weakSelf.view).offset(-15);
        make.height.mas_equalTo(50);
    }];
}

- (void)modifyButtonAction{
    [self.textField resignFirstResponder];
    if (k_Is_Empty(self.textField.text)) {
        [self showErrorTipWithTitle:@"请输入用户昵称"];
        return;
    }
    if (self.textField.text.length>10) {
        [self showErrorTipWithTitle:@"昵称过长"];
        return;
    }
    if (self.textField.text.length < 2) {
        [self showErrorTipWithTitle:@"昵称过短"];
        return;
    }
    self.nickname = self.textField.text;
  
    NSDictionary * dict = @{@"member_id":K_UserDefault_Read(CustomerUserID)?:@"",
                            @"member_token":K_UserDefault_Read(CustomerUserToken)?:@"",
                            @"member_real_name":@"",
                            @"member_img":self.imgUrl?:@"",
                            @"member_nick_name":self.nickname,
                            @"member_birthday":@"",
                            @"member_sex":@""};
    AutoWeakSelf
    [[QYNetManager sharedManager]POST:kUpdateMemberDetail parameters:dict andApiTyp:CustomerApiTypMain SuccessBlock:^(id responseObject) {
        if ([responseObject[@"status"] isEqualToString:@"ok"]) {
            if ([responseObject[@"data"] integerValue]) {
                [weakSelf showTipWithTitle:@"修改成功"];
                if (weakSelf.nickNameBlock) {
                    weakSelf.nickNameBlock(weakSelf.nickname,weakSelf.imgUrl);
                }
                [weakSelf.navigationController popViewControllerAnimated:YES];
            }else{
                [weakSelf showErrorTipWithTitle:@"修改失败\n请重试"];
            }
            
        }else{
            [weakSelf showTipWithTitle:responseObject[@"error"]];
        }
        
    } Failure:^(NSError *error) {
        [weakSelf showErrorTipWithTitle:CustomerPoolNetStr];
    }];
    
}

- (UITextField *)textField{
    if (!_textField) {
        _textField = [[UITextField alloc]initWithFrame:CGRectMake(25, NAVIGATION_BAR_HEIGHT + 60,F_DEVICE_W - 50, 40)];
        _textField.delegate = self;
        _textField.placeholder = @"请输入用户昵称";
        _textField.font = Font30;
        _textField.layer.borderColor = [HSPublic colorWithHexString:@"#45ACF9"].CGColor;
        _textField.layer.borderWidth = 1;
        _textField.layer.cornerRadius = 10;
        _textField.layer.masksToBounds = YES;
    }
    return _textField;
}

#pragma mark - UITextFieldDelegate
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
    return YES;
}

- (void)textFieldDidEndEditing:(UITextField *)textField{
    
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
